var utils = require("../utils/apis");
var des = require("../utils/des");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    code: '',
    jf_token: true,
    codeInput: '',
    key: '',
    loginType: '',
    errorMsg: ''
  },
  goList (e) {
    wx.navigateTo({
      url: `/sub_jinfu/${e.currentTarget.dataset.name}/${e.currentTarget.dataset.name}`,
    });
  },
  goBack () {
    wx.navigateBack({
      delta: 1, // 返回上一级页面。
    });
  },
  async setToken () {
    // 获取token
    const params = {
      // tenantId: '000000',
      // username: 'csyh001',
      // password: 'd34854e0799f78902a5ab340dc1d9dde',
      // grant_type: 'captcha',
      // scope: 'all',
      // type: 'account',
      // userType: 2
    }
    const res = await utils.apis(
      "/api/blade-auth/oauth/token?tenantId=000000&username=csyh001&password=830a24cf3507932ef27c94e886c176d4&grant_type=captcha&scope=all&type=account&userType=2",
      params,
      "POST",
      true,
      {
        'Blade-Auth': '',
        'Captcha-Code': this.data.codeInput,
        'Captcha-Key': this.data.key,
        'Tenant-Id': '000000'
      }
    );
    console.log(res, '金服的登录')
    if (res.statusCode !== 200) {
      this.getCode()
      wx.showToast({
        title: res.data.error_description,
        icon: 'error'
      })
    } else {
      // 判断是哪个页面进来的
      let pages = getCurrentPages();
      let prevpage = pages[pages.length - 2];
      console.log(prevpage.route, '过来的页面路由')
      wx.setStorageSync('jf_token', res.access_token)
      if (prevpage.route == 'pages/shouye/shouye') {
        this.setData({
          jf_token: true
        })
      } else {
        wx.navigateBack({ delta: 1 });
      }
    }
  },
  async getCode () {
    this.setData({
      code: '',
      codeInput: '',
      key: ''
    })
    const params = {}
    const res = await utils.apis(
      "/api/blade-auth/oauth/captcha",
      params,
      "GET",
      true,
      {
        'Blade-Auth': '',
        'Tenant-Id': '000000',
      }
    );
    console.log(res, 'code')
    this.setData({
      code: res.image || '',
      key: res.key || ''
    })
  },
  async goLogin () {
    const userInfo = wx.getStorageSync('userInfo')
    const params = {}
    const phone = encodeURIComponent(des.encrypt(userInfo.phone, "98fbffd1064a4355b8abaacb6fa96f94").data)
    // const financeCode = '29ea0acc5b5a448fa8422303b6b67540'
    const financeCode = userInfo.agencyCode
    const res = await utils.apis(
      `/api/blade-auth/oauth/token?tenantId=000000&phone=${phone}&grant_type=feature&scope=all&userType=2&financeCode=${financeCode}`,
      params,
      "POST",
      true,
      {
        'Blade-Auth': '',
        'Tenant-Id': '000000',
        'Grant-Type': 'feature'
      }
    );
    console.log(res, '金服的登录')
    if (res.statusCode == 400) {
      // wx.showToast({
      //   title: res.data.error_description,
      //   icon: 'none'
      // })
      this.setData({
        loginType: '1',
        errorMsg: res.data.error_description
      })
    } else {
      wx.setStorageSync('jf_token', res.access_token)
      this.setData({
        loginType: '2',
      })
    }
  },
  codeInput (e) {
    this.setData({
      codeInput: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    // console.log(des.decrypt(
    //   'cGMpIyQuToc+n4YY+LqMKQ==',
    //   "98fbffd1064a4355b8abaacb6fa96f94"
    // ), '解密得结果')
    // console.log(encodeURIComponent('18852956682'))
    // console.log(encodeURIComponent(des.encrypt('18852956682', "98fbffd1064a4355b8abaacb6fa96f94").data))
    // const token = wx.getStorageSync('jf_token') || ''
    // if (!token) {
    //   this.setData({
    //     jf_token: false
    //   })
    //   // 获取验证码
    //   this.getCode()
    // }
    // 静默登录
    this.goLogin()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    // this.setToken()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {
    this.setData({
      pageNum: ++this.data.pageNum
    })
    this.getInfos()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () { },
});
